<template>
  <div class="xtx-checkbox" @click="changeChecked()">
    <i v-if="checked" class="iconfont icon-checked"></i>
    <i v-else class="iconfont icon-unchecked"></i>
    <span v-if="$slots.default">
      <slot />
    </span>
  </div>
</template>
 <script setup >
import { ref, watchEffect } from 'vue';
import { useVModel } from '@vueuse/core'

const props = defineProps({
  modelValue: {
    type: Boolean,
    default: () => false
  }
})

// 方案 1  @vueuse/core
const emit = defineEmits(["update:modelValue", "change"])
const checked = useVModel(props, 'modelValue', emit)
const changeChecked = () => {
  const newVal = !checked.value
  // 通知父组件
  checked.value = newVal
  // 让组件支持change事件
  emit('change', newVal)
}



// 方案 2  自己手写

// let checked = ref(false)
// const emit = defineEmits(["update:modelValue"])
// watchEffect(() => {
//   checked.value = props.modelValue
// })
// const changeChecked = () => {
//   checked.value = !checked.value
//   emit("update:modelValue", checked.value)
// }
</script>
<style scoped lang="less">
.xtx-checkbox {
  display: inline-block;
  margin-right: 2px;

  .icon-checked {
    color: @xtxColor;

    ~span {
      color: @xtxColor;
    }
  }

  i {
    position: relative;
    top: 1px;
  }

  span {
    margin-left: 2px;
  }
}
</style>
